def initDict():
    info = {'name': '大海', 'age': 18}
    print('info: ', info)
    dic = dict(x=1, y=2)
    print('dic: ', dic)


# initDict()


def crud():
    info = {'name': '大海', 'age': 7, 'class': '3班', 'sex': '女'}
    print('info: ', info)
    info['addr'] = '天地任走'
    info.update({'age': 18, 'height': 175})  # 批量修改、新增kv
    print('update: ', info)
    info.setdefault('class', '6年级3班')
    print('setdefault class: ', info)
    info.setdefault('not_key', 'some_val')
    print('setdefault not_key: ', info)
    print('len(info): ', len(info))
    del info['name']
    # del info['aaa']    # 不存在的key会报错
    print('del info[\'name\']: ', info)
    print('pop(age): ', info.pop('age'))  # 不存在的key会报错
    print('pop(age) -> info: ', info)
    print('info.popitem(): ', info.popitem())  # 以元组形返回删除的元素
    print('info.popitem() -> info: ', info)
    print('type: ', type(info))
    # 查
    print(info['sex'])
    # 查一个不存在的key会报错
    # print(info['xxx'])
    print(info.get('sex'))
    # 没有key就返回None，不会报错
    print(info.get('xxxx'))
    # 取出所有的key
    print(list(info.keys()))
    # 取出所有的值
    print(list(info.values()))
    # 取出所有的键值对
    print(list(info.items()))



crud()
